<?php

namespace App\Http\Admin\Controllers\Media\Qingyun;

use App\Http\Admin\Services\Media\BliUper\BliUperService;
use App\Http\Models\BliUper;
use App\Http\Controllers\Controller;
use App\Http\Models\Qingyun;
use App\Lib\PHPExcel\ExcelHandle;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class QingyunController extends Controller
{
    public function index(Request $request){

        return view('media.qingyun_list');

    }


    public function list(Request $request){
        $attributes =   [];
        $attributes['start_time']     =   $request->input('start_time') ?? '';

        $attributes['end_time']       =      $request->input('end_time') ?? '';

        $attributes['title']          =   $request->input('title') ?? '';

        $attributes['author']         =   $request->input('author') ?? '';

        $attributes['page_size']      =   $request->input('page_size') ?? 30;

        $data   =   $this->queryData($attributes);


        return res('ok',$data);

    }



    public function qingyunExp(Request $request){

        $attributes =   [];
        $attributes['start_time']     =   $request->input('start_time') ?? '';

        $attributes['end_time']       =      $request->input('end_time') ?? '';

        $attributes['title']          =   $request->input('title') ?? '';

        $attributes['author']         =   $request->input('author') ?? '';


        $attributes['page_size']      =   $request->input('page_size') ?? '';

        $data   =   $this->queryData($attributes);

        $arr = $data ?? [];

        $title = '青云表格';
        ExcelHandle::factory('Qingyun', $title, $arr)->export();
    }



    public function queryData($attributes   =   []){


        $data   =   Qingyun::select('*');



        if(!empty($attributes['start_time']) && !empty($attributes['end_time'])){
            $data   =   $data->whereBetween('pub_date',[$attributes['start_time'],$attributes['end_time']]);
        }


        if(!empty($attributes['title'])){
            $data   =   $data->where('title','like',"%".$attributes['title']."%");
        }

        if(!empty($attributes['author'])){

            $data   =   $data->where('author','like',"%".$attributes['author']."%");

        }

        if(!empty($attributes['page_size'])){
            $data   =   $data->paginate($attributes['page_size']);
        }else{
            $data   =   $data->get();
        }



        return $data;


    }

}

